/*
 * SpringDamper.cpp
 *
 *  Created on: 18/05/2011
 *      Author: jorge
 */

#include "SpringDamper.h"

using namespace std;


namespace physics {

SpringDamper::SpringDamper() {
	left = right = NULL;
	L = ks = kd = 0.0;
}

SpringDamper::SpringDamper(Particle *left, Particle *right, double ks, double kd) {
	this->left = left;
	this->right = right;

	L = (left->getPosition() - right->getPosition()).largo();

	this->setDamping(kd);
	this->setRigidity(ks);
}

SpringDamper::~SpringDamper() {

}

SpringDamper::SpringDamper(Particle *left, Particle *right) {
	this->left = left;
	this->right = right;

	L = (left->getPosition() - right->getPosition()).largo();

}

void SpringDamper::setDamping(double kd) {
	if (kd >= 0.0 && kd <= 1.0) {
		this->kd = kd;
	} else {
		cerr << "Valor de amortiguación invalido" << endl;
	}
}

void SpringDamper::setRigidity(double ks) {

	if (ks >= 0.0 && ks <= 1.0) {
		this->ks = ks;
	} else {
		cerr << "Valor de rigidez invalido" << endl;
	}
}

double SpringDamper::getLenght() {
	return L;
}

double SpringDamper::getDamping() {
	return kd;
}

double SpringDamper::getRigidity() {
	return ks;
}

void SpringDamper::acomulateForce() {
	Vector3D f, r, v;
	double fs;

	r = (left->getPosition() - right->getPosition());
	v = (left->getVelocity() - right->getVelocity());

	fs = ks * (r.largo() - L) + kd * (v * r / r.largo());
	r.normalize();
	f = fs * r;

	right->addForce(f);
	left->addForce(-f);

}

}
